javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="https://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.8.2",function($,L){

// Adapted from http://jsfiddle.net/TRr6C/4/
function getCellLocation(cell) {

    var cols = cell.closest("tr").children("td").index(cell);
    var rows = cell.closest("tbody").children("tr").index(cell.closest("tr"));
    var coltemp = cols;
    var rowtemp = rows;

    c = cell.prevAll("td");
    cell.parent("tr").prevAll("tr").each(function() {
        //get row index for search cells
        var rowindex = cell.closest("tbody").children("tr").index($(this));
        // assign the row to a variable for later use
	row = $(this);

        row.children("td").each(function() {
            // fetch all cells of this row
            var colindex = row.children("td").index($(this));
            //check if this cell comes before our cell
            if (cell.offset().left > $(this).offset().left) {
        	// check if it has both rowspan and colspan, because the single ones are handled before
                var colspn = parseInt($(this).attr("colspan"));
                var rowspn = parseInt($(this).attr("rowspan"));
                if (rowspn) {
                    if(rowindex + rowspn > rows)
                    cols += 1;
                }
           }
	});
    });

    return {
        rows: rows,
        cols: cols
    };
}



// XXX verify MONTH view

var days = [];
var bal = 0;

var rowi=0;
var day_ops = [];
$("div.month-row").each(function() {

	$(this).find("td.st-c").each(function(id, obj) {
	$(obj).find(".rb-ni").each(function(id2, obj2) {
		loc = getCellLocation($(obj));
		id = (rowi*7)+loc.cols;

		if (days[id] == undefined) {
			days[id] = 0;
			day_ops[id] = [];
		}

		str = $(obj2).text();
		str = str.replace("$","").split(" ");
		var n = day_ops[id].length;
		day_ops[id][n] = str;
	});
	});

	rowi++;
});

for (var i=0; i < day_ops.length; i++) {
	if (day_ops[i] == undefined) continue;

	for (var j=0; j < day_ops[i].length; j++) {
		var str = day_ops[i][j];
		var id = i;

//		console.log("BAL="+bal);

		switch (str[0]) {
		case "IN":
//			console.log("IN+"+parseInt(str[1]));
			bal += parseInt(str[1]);
			days[id] = bal;
			break;
		case "OUT":
//			console.log("OUT-"+parseInt(str[1]));
			bal -= parseInt(str[1]);
			days[id] = bal;
			break;
		case "BAL":
//			console.log("SET BAL="+parseInt(str[1]));
			bal = parseInt(str[1]);
			days[id] = bal;
		}	

//		console.log("BAL="+bal);
	}
}

$('.st-dtitle').each(function(id, obj) {
	if (days[id] != undefined) {
		if ($(obj).find("span#bal_"+id)[0] == undefined) 
			$(obj).append("<span id='bal_"+id+"' style='color:black;font-weight:bold;'>&nbsp; (Balance: " + days[id] + ")</span>");
		// if bal_id then edit
		else {
			if (days[id] < 0) $(obj).find("span#bal_"+id).css("color", "red");
			else $(obj).find("span#bal_"+id).css("color", "black");
			$(obj).find("span#bal_"+id).text(" (Balance: "+days[id]+")");
		}
	}
});

});
